import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../models/theme.dart';
import 'package:provider/provider.dart';

class SettingsPage extends StatefulWidget {
  @override
  _SettingsPageState createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  // 这里可以添加状态变量，例如是否启用深色主题的开关状态

  @override
  Widget build(BuildContext context) {
    final themeModel = Provider.of<ThemeModel>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('设置'),
        automaticallyImplyLeading: false,
        centerTitle: true,
      ),
      body: ListView(
        children: <Widget>[
          SwitchListTile(
            title: Text('深色主题'),
            value: themeModel.isDarkMode, // 使用ThemeModel的状态控制开关
            onChanged: (bool value) {
              themeModel.toggleTheme(); // 调用ThemeModel的方法切换主题
              setState(() {}); // 触发界面重建以显示新的主题状态
            },
          ),
          ListTile(
            title: Text('清除缓存'),
            trailing: Icon(Icons.delete),
            onTap: () {
              // 实现清除缓存的逻辑
              // 可以弹出确认对话框后再执行
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: Text('确认清除？'),
                    content: Text('确定要清除缓存吗？'),
                    actions: <Widget>[
                      TextButton(
                        child: Text('取消'),
                        onPressed: () => Navigator.of(context).pop(),
                      ),
                      TextButton(
                        child: Text('确定'),
                        onPressed: () {
                          Navigator.of(context).pop(); // 关闭对话框
                        },
                      ),
                    ],
                  );
                },
              );
            },
          ),
          ListTile(
            title: Text('注销登录'),
            trailing: Icon(Icons.expand_circle_down),
            onTap: () {
              // 实现清除缓存的逻辑
              // 可以弹出确认对话框后再执行
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: Text('确认注销？'),
                    content: Text('注销会丢失所有数据，是否继续？'),
                    actions: <Widget>[
                      TextButton(
                        child: Text('取消'),
                        onPressed: () {
                          Navigator.of(context).pop(); // 关闭对话框
                        },
                      ),
                      TextButton(
                        child: Text('确定'),
                        onPressed: () => Navigator.pushNamed(context, '/'),
                      ),
                    ],
                  );
                },
              );
            },
          ),

          ListTile(
            title: Text('退出应用'),
            trailing: Icon(Icons.exit_to_app),
            onTap: () {
              // 点击时调用此方法退出应用
              SystemNavigator.pop();
            },
          ),
          // 添加更多设置项...
        ],
      ),
    );
  }
}
